import { reactive, ref } from "vue";
import { Api } from "../../../api/api.js";
import { ElNotification } from 'element-plus';
import { GJ_nowTime } from "../../../../static/js/tool.js";
function dataListJS() {
    let status = reactive({
        findData: {
            user:"",
            limit: 12,
            page: 1,
            pages: "",
            total: "",
            createName:null
        },
        //详细信息弹窗
        findDetais: false,
        // 编辑数据信息
        isChange: true,
        //修改后的信息
        rowData: [],
        // 用户信息
        userinfo: JSON.parse(window.sessionStorage.getItem("userinfo")),

    })
    // 数据列表
    let datalist = ref([]);
    let methodFun = {
        //获取数据
        getUserData: () => {
            if(status.userinfo.luckMeet){
                status.findData.user = [status.userinfo.user,status.userinfo.toUser];
            }else{
                status.findData.user = status.userinfo.user;
            }
            Api.getData(status.findData).then((res) => {
                let resData = res.data.data;
                console.log(res.data);
                status.findData.limit = res.data.limit;
                status.findData.page = res.data.page;
                status.findData.pages = res.data.pages;
                status.findData.total = res.data.total;
                datalist.value = resData;
            })
        },
        //修改数据
        isChangeFun: (user) => {
            console.log(user, "????", status.userinfo.user);
            if (user == status.userinfo.user || status.userinfo.limit == 0) {
                status.isChange = !status.isChange;
                if (status.isChange) {
                    if (status.rowData[0].isChange != "修改原因：好像没被修改哦") {
                        status.rowData[0].changeTime = GJ_nowTime("date-time", "-");
                        status.rowData[0].changeName = status.userinfo.userName;
                        status.rowData[0].limit = status.userinfo.limit;
                        Api.dataChange(status.rowData[0]).then((res) => {
                            if (res.data) {
                                ElNotification({
                                    type: "success",
                                    title: "数据已更新",
                                    message: "数据更新成功",
                                    duration: 1000,
                                });
                                methodFun.getUserData();
                                Api.log({ user: status.userinfo.user, createTime: GJ_nowTime("date-time", "-"), type: 2 })
                                    .then((res) => {
                                        console.log(res.data, "log");
                                    });
                            }
                        });
                    } else {
                        ElNotification({
                            type: "warning",
                            title: "请说明修改原因",
                            message: "请填写修改原因！",
                            duration: 1000,
                        });
                        methodFun.getUserData();
                    }
                }
            } else {
                ElNotification({
                    type: "error",
                    title: "权限错误",
                    message: "创建人不是你、你没有权限修改该数据",
                    duration: 1000,
                });
            }
        },
        //查看详细信息
        detailFun: (index, item) => {
            console.log(index, item);
            status.rowData.shift();
            status.rowData.push(item);
            status.findDetais = true;
            console.log(status);
        },
        //删除数据
        removeFun: (index, item) => {
            let createName = item.createName;
            let title = item.title;
            let createTime = item.createTime;
            let user = item.user;
            if ((user!="linxuan" && user!="wuyun123" && user==status.userinfo.user) ||  status.userinfo.limit == 0) {
                Api.dataRemove({ createName, title, createTime })
                    .then((res) => {
                        if (res.data) {
                            ElNotification({
                                type: "success",
                                title: "操作成功",
                                message: "数据已成功删除",
                                duration: 1000,
                            });
                            setTimeout(() => {
                                methodFun.getUserData();
                            }, 500);
                        }
                    });
            } else {
                ElNotification({
                    type: "error",
                    title: "权限错误",
                    message: "？？？你在干什么、这不是你发的！！！",
                    duration: 1000,
                });
            }
        },
        changePage: (page) => {
            console.log(page);
            status.findData.page = page;
            methodFun.getUserData();
        }
    }
    return {
        status,
        datalist,
        methodFun
    }
}

export default dataListJS;